package com.ysyc.driver_ee.activity;

import android.graphics.Color;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.base.BaseActivity;
import java.text.DecimalFormat;
import java.util.ArrayList;


/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/10/30 17:32<br>
 * 版本： v1.0<br>
 */
public class TestActivity extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_test;
    }

    @Override
    protected void initView() {
        BarChart mBarChart = (BarChart) findViewById(R.id.bar_chart);

        setBarChartStyle(mBarChart);

        // 制作10个数据点。
        setData(mBarChart, 10);
    }
    private void setBarChartStyle(BarChart mBarChart) {

        mBarChart.setDrawBarShadow(false);
        mBarChart.setDrawValueAboveBar(true);

        mBarChart.setMaxVisibleValueCount(60);
        mBarChart.setPinchZoom(false);
        mBarChart.setDrawGridBackground(false);

        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);


        YAxis leftAxis = mBarChart.getAxisLeft();
        leftAxis.setLabelCount(5, false);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        leftAxis.setTextColor(Color.BLUE);

        YAxis rightAxis = mBarChart.getAxisRight();
        rightAxis.setDrawGridLines(false);
        rightAxis.setLabelCount(5, false);
        rightAxis.setSpaceTop(15f);
        rightAxis.setTextColor(Color.GREEN);

        Legend mLegend = mBarChart.getLegend();
        mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
        mLegend.setForm(Legend.LegendForm.SQUARE);
        mLegend.setFormSize(15f);
        mLegend.setTextSize(12f);
        mLegend.setXEntrySpace(5f);
    }

    private void setData(BarChart mBarChart, int count) {

        ArrayList<String> xVals = new ArrayList<String>();
        for (int i = 0; i < count; i++) {
            xVals.add(i, i + "");
        }

        ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();

        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * 100);
            yVals.add(new BarEntry(val, i));
        }

        BarDataSet mBarDataSet = new BarDataSet(yVals, "柱状图测试数据");


        mBarDataSet.setBarBorderWidth(30f);


        // 柱状图柱的颜色
        mBarDataSet.setColor(Color.RED);

        // 当柱状图某一柱被选中时候的颜色
        mBarDataSet.setHighLightColor(Color.YELLOW);

        mBarDataSet.setValueFormatter(new IValueFormatter() {

            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                DecimalFormat decimalFormat = new DecimalFormat(".0");
                String s = decimalFormat.format(value) + "毫米";
                return s;
            }


        });

        ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
        dataSets.add(mBarDataSet);

        BarData mBarData = new BarData(dataSets.get(0),dataSets.get(1),dataSets.get(3));


        mBarData.setValueTextSize(12f);

        mBarChart.setData(mBarData);
    }
}
